//
//  MineNaviBar.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/10/29.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "MineNaviBar.h"
#import "MineItemModel.h"
@interface MineNaviBar ()
@property(nonatomic, strong) NSMutableArray<MineItemModel *> *itemModels;
@property(nonatomic, strong) UIButton *messageButton;
@end

@implementation MineNaviBar
- (void)makeUI {
    [super makeUI];
    self.backgroundColor = UIColor.clearColor;
    
    NSArray *dataList = @[ImageName.home_msg, ImageName.mine_setting, ImageName.mine_qcode];
    NSArray<NSNumber *> *actionList = @[@(ItemActionNaviMessage), @(ItemActionNaviSetting), @(ItemActionNaviQrCode)];
    CGFloat itemWidth = 22, itemMargin = 16;
    for (NSInteger index = 0; index < dataList.count; index ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button];
        button.frame = CGRectMake(kScreenWidth - (itemMargin + itemWidth) * (index + 1), kStatusBarHeight + (44 - itemWidth) * 0.5, itemWidth, itemWidth);
        [button setImage:[UIImage imageNamed:dataList[index]] forState:UIControlStateNormal];
        button.tag = index;
        [button addTarget:self action:@selector(clickItemButton:) forControlEvents:UIControlEventTouchUpInside];
        
        MineItemModel *itemModel = [[MineItemModel alloc]init];
        itemModel.action = actionList[index];
        [self.itemModels addObject:itemModel];
        
        if (index == 0) {
            self.messageButton = button;
        }
    }
}
- (void)handleEvent {
    
}
- (void)bindViewModel {
    [super bindViewModel];
    [RACObserve([TT_MessageManager manager], totalUnReadCount) subscribeNext:^(id x) {
        if ([TT_MessageManager manager].totalUnReadCount > 0) {
            self.messageButton.badgeValue = [NSString stringWithFormat:@"%d",[TT_MessageManager manager].totalUnReadCount];
            self.messageButton.badgeBGColor = [UIColor redColor];
        } else {
            self.messageButton.badgeValue = @"";
            self.messageButton.badgeBGColor = [UIColor clearColor];
        }
    }];
}
- (void)clickItemButton:(UIButton *)sender {
    !self.clickAction ?: self.clickAction(self.itemModels[sender.tag]);
}
#pragma mark - lazy
- (NSMutableArray<MineItemModel *> *)itemModels {
    if (!_itemModels) {
        _itemModels = [NSMutableArray array];
    }
    return _itemModels;
}
@end
